﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Core;
using GSLMS.Entity.Enum;
using GSLMS.Entity;

namespace GSLMS.WebAPP.Areas.DianDang.Controllers.Apply.SpecailInfo
{
    public class SpecailInfoController : AdminBaseController
    {
        protected override string navTabId
        { get { return "DianDang_Apply_SpecailInfo"; } }

        //
        // GET: /DianDang/Special/

        public ActionResult Index(FormCollection form)
        {
            var searchParma = new TB_APP_SPECIALINFO();
            //if (!searchParma.StarDateTime.HasValue)
            //{
            //    searchParma.StarDateTime = Convert.ToDateTime(DateTime.Now.AddMonths(-1).ToShortDateString());
            //    searchParma.EndDateTime = DateTime.Now;
            //}

            searchParma = base.SearchPara<TB_APP_SPECIALINFO>(form, searchParma);
            
            searchParma.STATUS = string.IsNullOrEmpty(Request["Status"]) ? -1 : Helper.ToInt(Request["Status"]);
            searchParma.StrSort = " id desc";

            ViewData["SpecialStatus"] = EnumHelper.ToSelectList<ApplyEnum.SpecialStatus>();


            var specailList = serviceSpecailInfo.GetList(searchParma) ?? new List<TB_APP_SPECIALINFO>();
            ViewData["RowCount"] = searchParma.RowCount;
            return View(specailList);
        }

        //
        // GET: /DianDang/Special/Details/5

        public ActionResult Details(int id)
        {
            var model = serviceSpecailInfo.GetByID(id);

            ViewData["auditInfo"] = serviceSpecail.GetAllByTypeAndPID(id, 5);

            return View(model);
        }

        //
        // GET: /DianDang/Special/Create

        public ActionResult Create(int? id)
        {
            var model = new TB_APP_SPECIALINFO();
            if (id.HasValue && id > 0)
            {
                model = serviceSpecailInfo.GetByID(id.Value);
            }
            ViewData["SpecialType"] = GetListFromDict(((int)SystemEnum.DistType.特批类型).ToString());
            var searchParm = new TB_APP_REGINFO()
            {
                SubAssignedIn = UniversallySearchParma.SubAssignedIn,
                Pagination = new Entity.DTO.PaginationDTO() { IsPaging = false }
              
            };

            searchParm.Pagination.PageSize = -1;

            ViewData["CustList"] = new SelectList(serviceRegInfo.GetList(searchParm), "ID", "CUSTNAME");

            ViewData["Product"] = new SelectList(serviceSysProduct.GetBySysetmID(Entity.Enum.SystemEnum.SystemID.典当, -1), "ID", "ProductName");// base.GetListFromDict("Product");
            return View(model);
        }

        [HttpPost]
        public JsonResult GetCustomerInfo(int? id)
        {
            var custModel = new TB_APP_REGINFO();
            if (id.HasValue)
            {
                custModel = serviceRegInfo.GetByID(id.Value);
            }
            return Json(custModel);
        }

        //
        // POST: /DianDang/Special/Create

        [HttpPost]
        public string Create(FormCollection form)
        {
            string result = string.Empty;
            TB_APP_SPECIALINFO model = new TB_APP_SPECIALINFO();
            TryUpdateModel(model, form);
            try
            {
                if (model.ID == 0)
                {
                    model.CREATEBY = CurentUser.UserName;
                    model.CREATEDATE = DateTime.Now;
                }
                else
                {
                    model.SPAREFIELD2 = CurentUser.UserName;
                    model.SPAREFIELD3 = DateTime.Now.ToString();
                }

                model.CUSTNAME = Request["HIDCUSTNAME"];

                bool isSumbit = model.ID > 0 ? serviceSpecailInfo.Update(model) : serviceSpecailInfo.Add(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }
            return "{" + result + "}";
        }


        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /DianDang/Special/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
